package util.core.plugins.mycraft.cc;

import java.io.FileNotFoundException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.bukkit.craftbukkit.libs.jline.internal.Log;

import implate.core.plugins.mycraft.cc.Mycraft;
import implate.core.plugins.mycraft.cc.SQLOperImplate;
import entity.core.plugins.mycraft.cc.config;
import entity.core.plugins.mycraft.cc.file;
import entity.core.plugins.mycraft.cc.SqlLink;

/**
 * 数据库操作类, 提供数据库的 增删查改 等功能 Fetch 获取单条数据 FetchAll 获取全部数据 Update 更新数据 Insert 插入数据
 * Delete 删除数据
 * 
 * @author delovt
 *
 */
public class SQLOper extends Mycraft implements SQLOperImplate {
	private DBConn db = null;
	private SqlLink ps = null;
	private ResultSet rs = null;
	private GenMySQLCommand gs = null;
	/**
	 * 数据库语句安全状态检查
	 */
	private boolean isSafe = false;

	public SQLOper(DBConn s) {
		this.db = s;
		this.GetCon();
	}

	public SQLOper(config cf) {
		if (cf != null) {
			this.G_cf = cf;
			this.db = new DBConn(cf);
			this.GetCon();
		} else {
			Log.warn("config file is null");
		}
	}

	public SQLOper(file fs) throws FileNotFoundException {
		this.db = new DBConn((config) new LoadBean(fs, new config()).getVariabled());
		this.GetCon();
	}

	public SQLOper(FileOper fs) throws FileNotFoundException {
		this.db = new DBConn((config) new LoadBean(fs.fs, new config()).getVariabled());
		this.GetCon();
	}

	/**
	 * 需要传入 config 文件路径
	 * 
	 * @param url
	 *            配置文件的路径
	 * @throws FileNotFoundException
	 */
	public SQLOper(String url) throws FileNotFoundException {
		this.db = new DBConn((config) new LoadBean(new FileOper(url).fs, new config()).getVariabled());
		this.GetCon();
	}

	@Override
	public void GetCon() {
		// TODO Auto-generated method stub
		if (this.db == null) Log.error("数据库连接错误");
		else {
			this.ps = this.db.getSql();
			this.setRs(null);
			this.gs = new GenMySQLCommand(this.ps, this.G_cf);
			this.SafeCheck();
		}
	}

	@Override
	public void CloseCon() {
		// TODO Auto-generated method stub
		this.db.CloseCon();
	}

	@Override
	public boolean Security(String str) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void SafeCheck() {
		// TODO Auto-generated method stub
		if (this.ps != null && this.ps.getLink() != null) {
			this.isSafe = true;
		}
	}

	@Override
	public boolean Exec() throws SQLException {
		// TODO Auto-generated method stub
		this.ps.setCommand(this.gs.getSqlCommand());
		int line = (-1);
		if (this.ps.getCommand() != null) line = this.ps.getCommand().executeUpdate();
		else
			Log.error("SQL Exec: SqlCommand is null");
		this.ps.setCommand(null);
		if (line != -1) return true;
		return false;
	}

	@Override
	public boolean ExecQuery() throws SQLException {
		// TODO Auto-generated method stub
		this.ps.setCommand(this.gs.getSqlCommand());
		if (this.ps.getCommand() != null) {
			this.ps.setResult(this.ps.getCommand().executeQuery());
			this.rs = this.ps.getResult();
			this.ps.setCommand(null);
			this.ps.setResult(null);
			return true;
		} else
			Log.error("SQL ExecQuery: SqlCommand is null");
		return false;
	}

	@Override
	public void Fetch(List<String> str) throws SQLException {
		// TODO Auto-generated method stub
		if (this.isSafe) {
			if (str.size() >= 4) str.set(4, "0,1");
			else {
				List<String> s = new ArrayList<String>();
				for (String a : str)
					s.add(a);
				s.add("0,1");
				str = s;
			}
			this.gs.MakeSearch(str);
			this.ExecQuery();
		}
	}

	public void CusExec(String sql) throws SQLException {
		if (this.isSafe) {
			this.gs.MakeExec(sql);
			this.Exec();
		}
	}

	@Override
	public void FetchAll(List<String> str) throws SQLException {
		// TODO Auto-generated method stub
		if (this.isSafe) {
			this.gs.MakeSearch(str);
			this.ExecQuery();
		}
	}

	@Override
	public boolean Insert(List<String> str) throws SQLException {
		// TODO Auto-generated method stub
		if (this.isSafe) {
			this.gs.MakeInsert(str);
			return this.Exec();
		}
		return false;
	}

	@Override
	public boolean Delete(List<String> str) throws SQLException {
		// TODO Auto-generated method stub
		if (this.isSafe) {
			this.gs.MakeDelete(str);
			return this.Exec();
		}
		return false;
	}

	@Override
	public boolean Update(List<String> str) throws SQLException {
		// TODO Auto-generated method stub
		if (this.isSafe) {
			this.gs.MakeUpdate(str);
			return this.Exec();
		}
		return false;
	}

	public ResultSet getRs() {
		return rs;
	}

	public void setRs(ResultSet rs) {
		this.rs = rs;
	}

	public boolean IsSafe() {
		return this.isSafe;
	}
}
